package hua.mtreader.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

import hua.mtreader.feature.login.bean.UserBean;

/**
 * Created by Administrator on 2016/3/22.
 */
public class ToolsPreferences {

    /**
     * 获取软件应用设置
     * 把用户信息储存在此shareperference中，此处增加读写方法，供其他模块调用
     */
    public static final String tdSettingPreferencesName = "user";
    public static final String TOKEN = "token"; //登录用户后的token
    public static final String NOT_LOGIN = "notLogin"; //登录用户后的token

    private static SharedPreferences mSharedPreferences;

    public static final String USER_TOKEN = "userToken";
    public static final String USER_ID = "userId";
    public static final String USER_AVATAR = "userAvatar";
    public static final String USER_LEVEL = "userLevel";
    public static final String USER_NAME = "userName";
    public static final String USER_SCORE = "userScore";


    public static void init(Context context) {
        mSharedPreferences = context.getSharedPreferences(tdSettingPreferencesName, Context.MODE_PRIVATE);
    }


    /**
     * 获取软件设置
     */
    public static SharedPreferences getTDSettingPreferences() {
        return mSharedPreferences;
    }


    /**
     * 编辑存储变量
     */
    public static void setPreferences(String key, String values) {
        SharedPreferences.Editor editor = getTDSettingPreferences().edit();
        editor.putString(key, values);
        editor.commit();
    }

    /**
     * 编辑存储变量
     */
    public static void setPreferences(String key, int values) {
        SharedPreferences.Editor editor = getTDSettingPreferences().edit();
        editor.putInt(key, values);
        editor.commit();
    }

    /**
     * 获取存储变量
     */
    public static int getPreferences(String key, int defaultValues) {
        return getTDSettingPreferences().getInt(key, defaultValues);
    }

    /**
     * 获取存储变量
     */
    public static String getPreferences(String key) {
        return getTDSettingPreferences().getString(key, "");
    }

    /**
     * 获取存储变量
     */
    public static long getPreferences(String key, long defaultValues) {
        return getTDSettingPreferences().getLong(key, defaultValues);
    }

    public static void setPreferences(String key, long values) {
        SharedPreferences.Editor editor = getTDSettingPreferences().edit();
        editor.putLong(key, values);
        editor.commit();
    }

    /**
     * 编辑存储变量
     */
    public static void setPreferences(String key, boolean values) {
        SharedPreferences.Editor editor = getTDSettingPreferences().edit();
        editor.putBoolean(key, values);
        editor.commit();
    }

    /**
     * 获取存储变量
     */
    public static boolean getPreferences(String key, boolean defaultValues) {
        return getTDSettingPreferences().getBoolean(key, defaultValues);
    }

    /**
     * 是否包含此键
     */
    public static boolean isContainKey(String key) {
        if (getTDSettingPreferences().contains(key)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否已登录
     * true 已登录
     * false 未登录
     *
     * @return
     */
    public static boolean isLogin() {
        return !NOT_LOGIN.equals(getTDSettingPreferences().getString(TOKEN, NOT_LOGIN));
    }

    /*
        把检测到的版本保存起来，下次检测的版本号如果和这个值相同。就不弹出提示升级的信息了
     */
    public static void setVersion(int version) {
        SharedPreferences.Editor editor = getTDSettingPreferences().edit();
        editor.putInt("version", version);
        editor.commit();
    }

    /*
     请求接口后得到的版本号，如果和上次的一样，就不弹提示了
     */
    public static boolean needShowUpgrade(int netVersion) {
        if (getTDSettingPreferences().contains("version")) {
            int version = getPreferences("version", 0);
            return netVersion > version;
        }
        return true;
    }

    public static void cleanUserInfo() {
        getTDSettingPreferences().edit().clear().commit();
    }

    public static void persistenceUser(UserBean user) {
        setPreferences(USER_TOKEN,user.getData().getToken());
        setPreferences(USER_ID,user.getData().getId());
        setPreferences(USER_AVATAR,user.getData().getIcon());
        setPreferences(USER_LEVEL,user.getData().getLevel());
        setPreferences(USER_NAME,user.getData().getName());
        setPreferences(USER_SCORE,user.getData().getScore());
    }
}
